/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import generate.lessonList.LessonList;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;

/**
 *
 * @author NgocNTP60573
 */
public class LessonListDAO {
    public List<LessonList> getListLessonByOptionId(String optionId){
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;

        try {
            con = connectData.makeConnect();
            String sql = "Select * from tbl_LessonList where OptionID=?";
            stm = con.prepareStatement(sql);
            stm.setInt(1, Integer.parseInt(optionId));
            rs = stm.executeQuery();
            List<LessonList> list = new ArrayList<LessonList>();

            while (rs.next()) {
                LessonList lesson = new LessonList();
                lesson.setOptionId(BigInteger.valueOf(rs.getInt("OptionID")));
                lesson.setLessonName(rs.getString("LessonName"));
                lesson.setLessonId(BigInteger.valueOf(rs.getInt("LessonID")));
                list.add(lesson);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public List<LessonList> getAllLesson(){
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;

        try {
            con = connectData.makeConnect();
            String sql = "Select * from tbl_LessonList";
            stm = con.prepareStatement(sql);
            rs = stm.executeQuery();
            List<LessonList> list = new ArrayList<LessonList>();

            while (rs.next()) {
                LessonList lesson = new LessonList();
                lesson.setOptionId(BigInteger.valueOf(rs.getInt("OptionID")));
                lesson.setLessonName(rs.getString("LessonName"));
                lesson.setLessonId(BigInteger.valueOf(rs.getInt("LessonID")));
                list.add(lesson);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void updateLessonNameByID(String id, String name){
        Connection con = null;
        PreparedStatement stm = null;
        int Lessonid = 0;
        try {
            con= connectData.makeConnect();
            String sql = "update tbl_LessonList set LessonName=? where LessonID=?";
            stm= con.prepareStatement(sql);
            Lessonid = Integer.parseInt(id);
            stm.setString(1, name);
            stm.setInt(2, Lessonid);
            stm.executeUpdate();
        } catch (Exception e) {
            System.out.println(e);
        } finally{

                try {
                    if(con!=null){
                        con.close();
                    }if(stm!=null){
                        stm.close();
                    }
                } catch (SQLException ex) {
                    System.out.println(ex);
                }
        }
    }
}